home *** CD-ROM | disk | FTP | other *** search
- Path: news.chevron.com!news
- From: "W.R.Volz" <hwrvo@chevron.com>
- Newsgroups: comp.lang.c++
- Subject: Re: how slow are virtual functions?
- Date: Fri, 01 Mar 1996 09:03:19 -0600
- Organization: Chevron Petroleum Technology Company
- Message-ID: <313711B7.41C67EA6@chevron.com>
- References: <4gioqc$9f0@onlink3.onlink.net> <31344ACF.1ABB@bbn.hp.com>
- NNTP-Posting-Host: hsun11.hou281.chevron.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; SunOS 4.1.3 sun4m)
-
- Manfred Lange wrote:
- >
- > wfss16@onlink.net wrote:
- > >
- > > Can anyone give me some timings/good anology as to how much slower calling a virtual
- > > function is as to a normal one?
- > >
- > > Thanks for your help!
- >
-
- I don't think what follows is a correct explanation. I don't think
- that any lookup is required, just a table access. Let me explain
- how I was told virtual functions are implemented:
-
- Assume that you have a base class A with two virtual functions,
- AA and AB. A new class is derived from A, called X with virtual
- functions, XX and XY. X also overrides the virtual function
- AA from the inherited A class. I every class that has virtual
- functions, there is a hidden pointer to what is called the vtable
- (or virtual table). Note that if the class has functions that are
- not virtual, no lookup is required, since the called function
- address is known at link time. The vtable is set up for class as
- follows:
-
- Class A:
- Data:
- Vtable -> address of AA
- address of AB.
-
- For class X:
- Data:
- vtable -> address of AA overridden in X
- address of AB
- address of XA
- address of XB.
-
- Note that the offset in the vtable is the same for the same
- virtual function in different classes. Note that AB in class
- X was not overridden, so it points to the Ab member in
- class A, just as it should.
-
- When a call to a virtaul function is made, the system loads
- the address of the vtable for the appropriate class and then from
- a fixed offset loads the address of the function and then
- calls the function. Hence if you call AA from class A, the vtable
- will be loaded for class A and the first address will be loaded
- and then called. If the real class is X, the table is still loaded
- but from class X this time, and the first entry is loaded and called.
- Note that there is only one copy of the vtable for each class
- while each instance of the classes has a single pointer to its vtable.
-
- The net result is that there is a very small runtime penalty for
- virtual functions that consists of a few load instructions. The
- overhead is constant for any virtual function and there is not
- lookup involved (that has already been done in the compiler).
- Note that in many cases, there are several calls to different
- functions in the class, so the vtable may alreay be loaded
- which means that the overhead is a single load instruction,
- after the first load of th vtable. This all is optimized by
- the compiler.
-
- I most likely have something wrong here, but it is probably of
- a minor nature so please excure me and I stand corrected
- in advance.
- > You will have a runtime overhead, that is caused by the fact the address of a virtual
- > function is determined during runtime. Each class has a address table (vtable) which
- > also contains the address of all virtual functions declared and defined for that
- > class. The runtime system has to look up the correct address depending on the type of
- > the object (the class the object is an instance of) and then calls the function. This
- > mechanism is also known as late binding.
- > For non-virtual functions, the binding can be done during link-time. This means that
- > no additional code has to be executed by the runtime-environment to determine the
- > address of the function to be executed.
- > So you will have a runtime overhead. But keep in mind, that the percentage of the
- > overhead in general is very small, as the code of the virtual function needs more time
- > to execute than the code for the overhead in most cases. Also the address-lookup is
- > implemented using hash-functions in most cases.
- > In the projects I was and still am working on, I never had the problem that calling
- > virtual functions needs too much processor time. (The first project was 300,000 lines
- > of code, 300 classes, took 2 years, the current project will have 200 classes, will
- > take approx. 6 months).
- > You can save more time by avoiding passing objects on the stack to the function. Use
- > pointers or references instead.
- >
- > Hope this helps.
- >
- > --
- > Regards, Manfred.
- >
-
- --
-
- ======================
- Bill Volz
- Chevron Petroleum Technology Co.
- Earth Model/Interpretation & Analysis Division.
- P.O. Box 42832, Houston, TX, 77242-2832
- Phone: (713) 596-2059 Fax: (713) 596-3009
-